﻿using System.Data;

namespace VitCode.Castle.Helper
{
    /// <summary>
    /// DataTableCheckerHelper 的摘要说明。
    /// </summary>
    public abstract class DataTableCheckerHelper
    {
        private EmptyColumnChecker m_NotEmptyColumn;
        private RepeatColumnChecker m_NotRepeatColumn;

        /// <summary>
        /// 构建检查器
        /// </summary>
        public DataTableCheckerHelper()
        {
            m_NotEmptyColumn = new EmptyColumnChecker(this);
            m_NotRepeatColumn = new RepeatColumnChecker(this);
        }

        /// <summary>
        /// 获取或设置需要检查不能为空的列的集合
        /// </summary>
        public EmptyColumnChecker NotEmptyColumns
        {
            get { return m_NotEmptyColumn; }
            set { m_NotEmptyColumn = value; }
        }

        /// <summary>
        /// 获取或设置不能重复的列并且不能为空的列的集合
        /// </summary>
        public RepeatColumnChecker NotRepleateColumns
        {
            get { return m_NotRepeatColumn; }
            set { m_NotRepeatColumn = value; }
        }

        /// <summary>
        /// 根据从Excel生成的DataTable中的行索引，获取
        /// Excel表格中的行数
        /// </summary>
        /// <param name="DataRowIndex"></param>
        /// <returns></returns>
        public int GetSourceDataIndex(int DataRowIndex)
        {
            if (HasTitle)
                return DataRowIndex + 2;
            else
                return DataRowIndex + 1;
        }

        /// <summary>
        /// 根据Excel表格中的行数获取 DataTable中的行索引
        /// </summary>
        /// <param name="RowNumber"></param>
        /// <returns></returns>
        public int GetRowsIndex(int RowNumber)
        {
            if (HasTitle)
                return RowNumber - 2;
            else
                return RowNumber - 1;
        }

        private bool m_HasTitle = true;

        /// <summary>
        /// 获取Excel档中是否有列头,这个属性会影响<see cref="GetSourceDataIndex"/>和<see cref="GetRowsIndex"/>的判断
        /// </summary>
        public virtual bool HasTitle
        {
            get { return m_HasTitle; }
            set { m_HasTitle = value; }
        }

        /// <summary>
        /// 获取需要被检查的DataTable
        /// </summary>
        /// <returns></returns>
        public abstract DataTable GetDataTable();

        /// <summary>
        /// 获取需要被检查的Title
        /// </summary>
        /// <returns></returns>
        public virtual DataColumnCollection GetCheckTitles()
        {
            return GetEmptyDataTableFromSource().Columns;
        }

        /// <summary>
        /// 从数据源中获取一个没有任何数据的数据源
        /// </summary>
        public abstract DataTable GetEmptyDataTableFromSource();

        /// <summary>
        /// 创建一个与数据源一样但是没有数据的DataTable
        /// </summary>
        /// <returns></returns>
        public virtual DataTable CreateEmptyDataTable()
        {
            return GetEmptyDataTableFromSource().Clone();
        }
    }
}
